<template>
  <div class="roleList">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>筛选搜索</span>
        <div class="btn" style="float: right">
          <el-button size="small" @click="formReset">重置</el-button>
          <el-button size="small" type="primary" @click="searchRole">查询搜索</el-button>
        </div>
        <el-form ref="form" :model="form" label-width="140px"  size="mini">
          <el-form-item label="输入搜索" style="width:300px" prop="name">
            <el-input v-model="form.name" placeholder="角色名称"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <el-button size="mini" @click="addRole">添加角色</el-button>
      <el-table
        :data="roleList"
        border
        style="width: 100%">
        <el-table-column
          align="center"
          prop="date"
          label="编号"
          type="index"
          width="100">
        </el-table-column>
        <el-table-column
          align="center"
          prop="name"
          label="角色名称">
        </el-table-column>
        <el-table-column
          align="center"
          prop="description"
          label="描述">
        </el-table-column>
        <el-table-column
          align="center"
          label="添加时间">
          <template slot-scope="scope">
            <span>{{scope.row.createdTime | tiemFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          fixed="right"
          label="操作"
          width="170">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="toAllocMenu(scope.row.id)">分配菜单</el-button>
            <el-button type="text" size="small" @click="toAllocResource(scope.row.id)">分配资源</el-button>
            <el-button type="text" size="small" @click="roleEdit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="roleDel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <add-role ref='addRole' :isEdit = 'isEdit' @refrshRole="getRole"></add-role>
  </div>
</template>

<script>
import { getRoleAll, getRolePages, getRoleDel } from '@/services/role'
import addRole from './addRole'
export default {
  name: 'roleList',
  components: {
    addRole
  },
  data () {
    return {
      form: {
      },
      // 表格渲染数据
      roleList: [],
      isEdit: false
    }
  },
  created () {
    this.loadRoleAll()
  },
  methods: {
    // 获取所有的角色列表
    async loadRoleAll () {
      const { data } = await getRoleAll()
      if (data.code === '000000') {
        this.roleList = data.data
      }
    },
    // 按条件查询角色
    async searchRole () {
      // 根据搜索的表单数据发送请求,从而将得到的数据渲染到table表格中
      const { data } = await getRolePages(this.form)
      if (data.code === '000000') {
        this.roleList = data.data.records
      }
    },
    // 重置表单,只有给每项表单元素设置了prop属性,才能生效
    formReset () {
      this.$refs.form.resetFields()
      this.loadRoleAll()
    },
    // 添加角色
    // 通过ref属性,可以使得父组件调用子组件的方法
    addRole () {
      this.isEdit = false
      this.$refs.addRole.dialogOpen()
    },
    // 编辑角色
    roleEdit (roleEdit) {
      this.isEdit = true
      this.$refs.addRole.dialogEdit(roleEdit)
    },
    // 当添加角色的弹窗保存后,刷新列表页
    getRole () {
      this.loadRoleAll()
    },
    // 删除列表的某一项
    roleDel (id) {
      this.$confirm('是否确定删除数据?', '提示')
        .then(async () => {
          const { data } = await getRoleDel(id)
          if (data.code === '000000') {
            this.$message.success('数据删除成功')
            const index = this.roleList.findIndex(item => item.id === id)
            this.roleList.splice(index, 1)
          }
          this.$message.success('删除成功!')
        }).catch(() => {
          this.$message.error('已取消删除')
        })
    },
    // 分配菜单
    toAllocMenu (id) {
      this.$router.push({ name: 'allocMenu', params: { id } })
    },
    // 分配资源
    toAllocResource (id) {
      this.$router.push({ name: 'allocResource', params: { id } })
    }
  },
  filters: {
    tiemFilter (val) {
      const data = new Date(val)
      function getDate (value) {
        if (value < 10) {
          return '0' + value
        }
        return value
      }
      return `${data.getFullYear()}-${getDate(data.getMonth() + 1)}-${getDate(data.getDate())}
              ${getDate(data.getHours())}:${getDate(data.getMinutes())}:${getDate(data.getSeconds())}
            `
    }
  }
}
</script>

<style lang='scss' scoped>
.el-form{
  margin-top: 8px;
}
::v-deep .el-card__body{
  padding: 20px 0 !important;
}
.el-table{
  margin-top: 20px !important;
}
.el-button{
  margin-left: 20px !important;
}
</style>
